package com.elite.groovy.callback

import com.elite.groovy.davco.clink.ClinkUtils
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.util.EliteBean
import com.fasterxml.jackson.core.JsonProcessingException
import com.tinet.clink.core.exceptions.ClientException
import com.tinet.clink.core.exceptions.ServerException
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat
/**
 * @projectName: Groovy
 * @package: com.elite.groovy.callback
 * @className: apiClinkMonitorAgent
 * @description: 天润坐席监控
 * @author: LiJT
 * @date: 2023-10-27 11:13
 * @version: 1.0
 */
public class apiClinkMonitorAgent implements ServiceBackInvoke {
    private static final Logger log = LoggerFactory.getLogger(apiClinkMonitorAgent.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat sdateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取数据源
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        // 获取当前时间
        Date date = new Date();
        String Update_Time = sdateFormat.format(date);

        // 获取所有Header
        Map<String, String> mapHeader = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            // log.debug("Call Header-" + key + ":" + value);

            mapHeader.put(key, value);
        }

        // 封装调用头Heads
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Content-Type", "application/json");

        // 系统接入-GET
        String getPath = "/cc/agent_status?";

        Map<String, String> queryParams = new HashMap<String, String>();
        String doGETRes = ClinkUtils.doClinkCall("GET", getPath, queryParams, headMap, null);
        // log.debug("系统接入 doGETRes:" + doGETRes);

        try {
            JSONObject doGETJSON = JSONObject.fromObject(doGETRes);
            // log.debug("天润监控返回坐席信息:" + doGETJSON);
            try {
                JSONArray agentArray = doGETJSON.getJSONArray("agentStatus");
                String qno = "--";

                // 变量初始化
                JSONObject agentJson = null;
                String cno = "";
                String clientName = "";
                String bindType = "";
                String bindTel = "";
                String customerNumber = "";
                String customerNumberEncrypt = "";
                String pauseType = "";
                String pauseDescription = "";
                String agentStatus = "";
                String agentStatusDetail = "";
                String incomingCallCount = "";
                String queueIncomingCallCount = "";
                String stateDuration = "";
                String loginDuration = "";
                String queuesMap = "";
                List<Map<String, Object>> agentSList = null;

                String updateSQL = "";
                String agentSQL = "";

                // 处理坐席成员agentStatus
                for (int j = 0; j < agentArray.size(); j++) {
                    agentJson = agentArray.getJSONObject(j);
                    cno = KHUtil.GetSFJV(agentJson, "cno");
                    clientName = KHUtil.GetSFJV(agentJson, "clientName");
                    bindType = KHUtil.GetSFJV(agentJson, "bindType");
                    bindTel = KHUtil.GetSFJV(agentJson, "bindTel");
                    customerNumber = KHUtil.GetSFJV(agentJson, "customerNumber");
                    customerNumberEncrypt = KHUtil.GetSFJV(agentJson, "customerNumberEncrypt");
                    pauseType = KHUtil.GetSFJV(agentJson, "pauseType");
                    pauseDescription = KHUtil.GetSFJV(agentJson, "pauseDescription");
                    agentStatus = KHUtil.GetSFJV(agentJson, "agentStatus");
                    agentStatusDetail = KHUtil.GetSFJV(agentJson, "agentStatusDetail");
                    incomingCallCount = KHUtil.GetSFJV(agentJson, "incomingCallCount");
                    queueIncomingCallCount = KHUtil.GetSFJV(agentJson, "queueIncomingCallCount");
                    stateDuration = KHUtil.GetSFJV(agentJson, "stateDuration");
                    loginDuration = KHUtil.GetSFJV(agentJson, "loginDuration");
                    queuesMap = KHUtil.GetSFJV(agentJson, "queuesMap");

                    // log.debug("当前监控坐席信息:" + cno + " " + clientName);
                    agentSQL = "Select Qno, Cno From Trc2_Agent_All_Monitor Where Qno = ? And Cno = ?";
                    agentSList = jt.queryForList(agentSQL, qno, cno);
                    if (agentSList.size() > 0) {
                        updateSQL = "Update Trc2_Agent_All_Monitor Set Clientname = ?, Bindtype = ?, Bindtel = ?, Customernumber = ?, Customernumberencrypt = ?, Pausetype = ?, Pausedescription = ?, Agentstatus = ?, Agentstatusdetail = ?, Incomingcallcount = ?, Queueincomingcallcount = ?, Stateduration = ?, Loginduration = ?, Update_Time = ?,queuesMap=? Where Cno = ? And Qno = ?";

                        jt.update(updateSQL, clientName, bindType, bindTel, customerNumber, customerNumberEncrypt, pauseType, pauseDescription, agentStatus, agentStatusDetail, incomingCallCount, queueIncomingCallCount, stateDuration, loginDuration, Update_Time, queuesMap, cno, qno);

                    } else {
                        updateSQL = "Insert Into Trc2_Agent_All_Monitor (Cno, Qno, Clientname, Bindtype, Bindtel, Customernumber, Customernumberencrypt, Pausetype, Pausedescription, Agentstatus, Agentstatusdetail, Incomingcallcount, Queueincomingcallcount, Stateduration, Loginduration, Update_Time,queuesMap) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";

                        jt.update(updateSQL, cno, qno, clientName, bindType, bindTel, customerNumber, customerNumberEncrypt, pauseType, pauseDescription, agentStatus, agentStatusDetail, incomingCallCount, queueIncomingCallCount, stateDuration, loginDuration, Update_Time, queuesMap);

                    }

                }

                String dealAgentSQL = "Delete From Trc2_Agent_All_Monitor Where Update_Time != ? And Qno = ?";
                jt.update(dealAgentSQL, Update_Time, qno);

            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

        } catch (ClientException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }

        return 1;
    }
}